﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core;
using Microhard.WeixinMP.Msg;
using Microhard.WeixinMP.Models;

namespace Microhard.WeixinMP.Impls
{
    public class SendToUserInfoImpl : ISendToUserInfo
    {
        public Core.DBUtility.ExecutedResult Add(Models.SendToUserInfoModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.BelongEntityID = this.scope.EmpInfo.CompanyID;

            try
            {
                db.MPInfoes.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, SendUserInfo.AddFailed);
                Log.Error("ISendToUserInfo.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.SendToUserInfoModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPInfoes.SingleOrDefault(l => l.InfoID == model.InfoID);
            if (entity != null)
            {
                entity.Content = model.Content;
                entity.CreateTime = DateTime.Now;
                entity.Title = model.Title;
                entity.Summary = model.Summary;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, SendUserInfo.UpdateFailed);
                Log.Error("ISendToUserInfo.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.MPInfoes.Where(l => ids.Contains(l.InfoID)).ToList();
            entity.ForEach(i =>
            {
                db.MPInfoes.DeleteObject(i);
            });
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, SendUserInfo.DeleteFailed);
                Log.Error("ISendToUserInfo.Delete", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.SendToUserInfoModel model)
        {
            throw new NotImplementedException();
        }

        public Models.SendToUserInfoModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var model = db.MPInfoes.SingleOrDefault(c => c.InfoID == id);
            if (model != null)
            {
                return model.ToModel();
            }
            return null;
        }

        public Models.SendToUserInfoModel GetModelByModel(Models.SendToUserInfoModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.SendToUserInfoModel> Query(Models.SendToUserInfoModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            IEnumerable<MPInfo> entities = from b in db.MPInfoes
                                           where this.scope.PKID.Contains(b.InfoID)
                                           select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<SendToUserInfoModel>();
        }

        public List<Models.SendToUserInfoModel> Query(Models.SendToUserInfoModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsMsgTypeDuplicate(string clickKey)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var cnt = db.MPInfoes.Count(c => c.ClickKey == clickKey);
            return cnt > 0 ? true : false;
        }
    }
}
